
import Taro from '@tarojs/taro';
import { View, Text } from '@tarojs/components';
import http from '@/utils/http';
import utils from '@/utils/util'

import DrugItem from '@/components/drugItem/index';
import StatusPages from '@/components/statusPages/index';
import MoreFooter from '@/components/moreFooter/index';

import './index.scss';

let app = Taro.getApp();
class DrugAdministration extends Taro.Component {
    constructor(props) {
        super(props);
        this.state = {
            drugList: [],
            moreType: 'none',
            statusType: 'none', // none 不显示空白页  empty //显示空白页
            statuserro: false, // 是否显示错误页
            allTotal: 0,
            erromessage:'',
            nearExpireTotal: 0
        }
    }
    componentDidShow() {
        this.getMedicalkit()
    }
    config = {
        navigationBarTitleText: '药品管理',
        enablePullDownRefresh: true
    }
    tapDetails(e, item) {
        Object.keys(item).map((items)=>{
            item[items] = encodeURIComponent(item[items])
        })
        Taro.navigateTo({
            url: '/pages/drugDetails/index?' + utils.queryconcat(item)
        })
    }
    refresh() { //下拉刷新
        var {
            moreType
        } = this.state;
        if (moreType != "loading") {
            this.setState({
                moreType: 'none'
            }, () => {
                    this.getMedicalkit();
            })
        }
    }
    onPullDownRefresh() {
        this.refresh();
    }
    getMedicalkitByIndex(id){
        http.get('getMedicalkitByIndex',{
            id
        }).then((res)=>{
            console.log(res)
        }).catch((e) => {
            console.log(e)
        })
    }
    getMedicalkit() {
        const { medicalkitid: MedicalkitId, SigninId} = this.$router.params
        let { moreType, drugList} = this.state
        let wardid = app.getValueSync('lastwardId');
        let medicalkitidStore = app.getValueSync("lastmedicalkitid");
        let Apiurl = 'getMedicalkit';
        let datas = {
            wardid,
            medicalkitid:medicalkitidStore
        };
        if (MedicalkitId) {
            Apiurl = 'getMedicalkitById'
            datas = {
                MedicalkitId
            };
        }
        if (SigninId) {
            Apiurl = 'getCheckDetail'
            datas = {
                id: SigninId
            };
        }
        http.get(Apiurl, datas).then((res) => {
            Taro.stopPullDownRefresh();
            let MedicalkitArr = res.datas;
            if (MedicalkitId) {
                MedicalkitArr = [res.datas];
            }
            let list = [];
            let allTotal=0, nearExpireTotal=0;
            MedicalkitArr.map((item)=>{
                allTotal += item.countAll;
                nearExpireTotal += item.countValidity;
                let rdata = JSON.parse(JSON.stringify(item));
                delete rdata['mark'];
                delete rdata['pics'];
                delete rdata['medicalkits'];
                item.medicalkits.map((items)=>{
                    list.push({
                        ...rdata,
                        ...items,
                        rmedicalkits: item.id
                    })
                })
            })
            moreType = "noMore";
            this.setState({
                moreType,
                allTotal,
                nearExpireTotal,
                statusType: list.length > 0 ? 'none' : 'empty',
                statuserro: false,
                drugList: list
            });
        }).catch((e) => {
            console.log(e);
            Taro.stopPullDownRefresh();
            this.setState({
                statusType: drugList.length > 0 ? 'none' : 'empty',
                statuserro: drugList.length > 0 ? false : true,
                erromessage: e&&e.datas || '网络错误,请点击屏幕重试'
            })
        })
    }
    render() {
        const { drugList, allTotal, nearExpireTotal, statusType, statuserro, moreType, erromessage} = this.state;
        return (
            <View className='drug-administration ov_h'>
                <View className='pl-3 pr-3 length-box ft28 flex flex-r flex-item-between flex-item-v-center mt-2-5'>
                    <Text className='ft_w600 fc_53C ft32'>全部药品<Text className='fc_1FF'>{allTotal}支</Text>，近效期药品<Text className='fc_960'>{nearExpireTotal}支</Text></Text>
                </View>
                <StatusPages mt='mt-20'
                  refresh={() => { this.refresh() }}
                  statusType={statusType}
                  statuserro={statuserro}
                  erromessage={erromessage}
                ></StatusPages>
                <View className='pl-2-5 pr-2-5 mt-2-5'>
                    {
                        drugList.map((item) => {
                            return (
                                <DrugItem drugItem={item} key={item.id} itemTap={(e, obj) => { !this.$router.params.medicalkitid && this.tapDetails(e, obj) }}></DrugItem>
                            )
                        })
                    }
                </View>
                <MoreFooter moreType={moreType} restloading={() => { this.getMedicalkit() }} ></MoreFooter>
            </View>
        );
    }
}

export default DrugAdministration;